﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class ColorToTabForegroundConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Brush darkBrush = StrictConverter.Convert<Brush>(values[0]);
            if (values[1] == null || values[1] == DependencyProperty.UnsetValue)
                return darkBrush;
            SpreadsheetColor color = StrictConverter.Convert<SpreadsheetColor>(values[1]);
            OpenLS.Drawing.ColorScheme colorScheme = StrictConverter.Convert<OpenLS.Drawing.ColorScheme>(values[2]);
            string hint = values[3] != null ? values[3].ToString() : string.Empty;
            Color actualColor = color.GetColor(colorScheme);
            switch (hint)
            {
                case "selected":
                    return darkBrush;
                case "hover":
                    {
                        var average = actualColor.R + actualColor.G + actualColor.B;
                        average = average / 3;
                        if (average < 50)
                            return Brushes.White;
                        return darkBrush;
                    }
                case "default":
                    default:
                    {
                        var average = actualColor.R + actualColor.G + actualColor.B;
                        average = average / 3;
                        if (average < 100)
                            return Brushes.White;
                        return darkBrush;
                    }
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}